Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mutex to getSnapState to prevent concurrent decryption #3234

Merged
merged 3 commits into from
Mar 25, 2025

Conversation

Mrtenz
Copy link
Member

@Mrtenz Mrtenz commented Mar 14, 2025

This wraps getSnapState in a mutex, to ensure we only decrypt once when calling it at the same time. The first call will decrypt the state and cache it in the Snaps runtime, and the next call will just use the cached result.

Copy link

codecov bot commented Mar 14, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.98%. Comparing base (92f8922) to head (64549e3).
Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3234   +/-   ##
=======================================
  Coverage   94.98%   94.98%           
=======================================
  Files         511      511           
  Lines       11300    11301    +1     
  Branches     1737     1737           
=======================================
+ Hits        10733    10734    +1     
  Misses        567      567           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Mrtenz Mrtenz marked this pull request as ready for review March 24, 2025 12:58
@Mrtenz Mrtenz requested a review from a team as a code owner March 24, 2025 12:58
@Mrtenz Mrtenz force-pushed the mrtenz/get-state-mutex branch from 40f1110 to 6c486b3 Compare March 24, 2025 13:02
@@ -2046,33 +2051,36 @@ export class SnapController extends BaseController<
*/
async getSnapState(snapId: SnapId, encrypted: boolean): Promise<Json> {
const runtime = this.#getRuntimeExpect(snapId);
const cachedState = encrypted ? runtime.state : runtime.unencryptedState;
return await runtime.getStateMutex.runExclusive(async () => {
Copy link
Member

@FrederikBolding FrederikBolding Mar 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this cached requests would still have to queue to access the cache, right? That seems less than ideal 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it work if we have a cacheMutex that nests this one?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by nesting in this case?

With this cached requests would still have to queue to access the cache, right? That seems less than ideal 🤔

Yeah, but cached access should be much quicker, so I don't think it's a big issue?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try this as-is and we can always move some logic outside of the mutex block

/**
* A mutex to prevent concurrent state decryption.
*/
getStateMutex: Mutex;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if we could re-use the stateMutex

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could, but I'm not sure if it should? We can do encryption and decryption at the same time, they don't depend on each other.

@Mrtenz Mrtenz force-pushed the mrtenz/get-state-mutex branch from 6c486b3 to 64549e3 Compare March 25, 2025 09:48
@Mrtenz Mrtenz enabled auto-merge March 25, 2025 09:51
@Mrtenz Mrtenz added this pull request to the merge queue Mar 25, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Mar 25, 2025
@FrederikBolding FrederikBolding added this pull request to the merge queue Mar 25, 2025
Merged via the queue into main with commit b421ca1 Mar 25, 2025
171 checks passed
@FrederikBolding FrederikBolding deleted the mrtenz/get-state-mutex branch March 25, 2025 10:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants